package com.harzone.commons.search.domain;

import java.io.Serializable;

import com.harzone.commons.search.visitor.FieldVisitor;

/**
 * @author ls
 * 2014年11月21日下午2:03:26
 */
public class Field implements Serializable,Cloneable{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	 
	//字段名称
	protected String name;
	//别名
	protected String alias;
	//值
	protected Object value;
	
	protected Float boost = 1.0F;
	
	//是否为主键
	protected boolean primaryKey = false;
	//是否为外键
	protected boolean foreignkey = false;
	
	//能否为空
	protected Boolean isNull = false;
	
	//是否为索引字段
	protected boolean createIndex = false;
	
	//是否存储字段
	protected boolean store = false;
	
	//类型
	protected FieldType fieldType;
	
	//长度
	protected int fieldLength = -1;
	
	//数据中的唯一键值名
	protected String indexName;
	
	protected String desc;
	
	protected Table table  = null;
	
	protected Field foreignkeyField = null;
	
	private String showName = null;
	
	public Field(String name) {
		this(name, name);
	}
	
	
	public Field(String name, String alias) {
		super();
		this.name = name;
		this.alias = alias;
		this.indexName = name;
	}
	
	
	public Field(String name, Object value) {
		this(name, name);
		this.value = value;
	}
	
	public String getName() {
		return name;
	}
	public Field setName(String name) {
		this.name = name;
		showName = null;
		return this;
	}
	public String getAlias() {
		return alias;
	}
	public Field setAlias(String alias) {
		this.alias = alias;
		showName = null;
		return this;
	}
	
	public Object getValue() {
		return value;
	}
	public Field setValue(Object value) {
		this.value = value;
		return this;
	}
	
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((alias == null) ? 0 : alias.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Field other = (Field) obj;
		if (alias == null) {
			if (other.alias != null)
				return false;
		} else if (!alias.equals(other.alias))
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}
	
	public Float getBoost() {
		return boost;
	}
	public Field setBoost(Float boost) {
		this.boost = boost;
		return this;
	}
	public Table getTable() {
		return table;
	}
	public Field setTable(Table table) {
		this.table = table;
		return this;
	}
	
	public Boolean getIsNull() {
		return isNull;
	}
	public Field setIsNull(Boolean isNull) {
		this.isNull = isNull;
		return this;
	}
	
	public boolean isStore() {
		return store;
	}
	public Field setStore(boolean store) {
		this.store = store;
		return this;
	}
	public FieldType getFieldType() {
		return fieldType;
	}
	public Field setFieldType(FieldType fieldType) {
		this.fieldType = fieldType;
		return this;
	}
	public int getFieldLength() {
		return fieldLength;
	}
	public Field setFieldLength(int fieldLength) {
		this.fieldLength = fieldLength;
		return this;
	}
	public String getIndexName() {
		return indexName;
	}
	public Field setIndexName(String indexName) {
		this.indexName = indexName;
		return this;
	}
	public String getDesc() {
		return desc;
	}
	public Field setDesc(String desc) {
		this.desc = desc;
		return this;
	}
	public static long getSerialversionuid() {
		return serialVersionUID;
	}
	public boolean isCreateIndex() {
		return createIndex;
	}
	public Field setCreateIndex(boolean createIndex) {
		this.createIndex = createIndex;
		return this;
	}
	public boolean isPrimaryKey() {
		return primaryKey;
	}
	public Field setPrimaryKey(boolean primaryKey) {
		this.primaryKey = primaryKey;
		return this;
	}
	public boolean isForeignkey() {
		return foreignkey;
	}
	public Field setForeignkey(boolean foreignkey) {
		this.foreignkey = foreignkey;
		return this;
	}
	@Override
	public String toString() {
		return "Field [name=" + name + ", alias=" + alias + ", value=" + value
				+ ", boost=" + boost + ", primaryKey=" + primaryKey
				+ ", foreignkey=" + foreignkey + ", isNull=" + isNull
				+ ", createIndex=" + createIndex + ", store=" + store
				+ ", fieldType=" + fieldType + ", fieldLength=" + fieldLength
				+ ", indexName=" + indexName + ", desc=" + desc + ", "
				+ (table!=null?table.string():null) + "]";
	}
	public Field getForeignkeyField() {
		return foreignkeyField;
	}
	public Field setForeignkeyField(Field foreignkeyField) {
		this.foreignkeyField = foreignkeyField;
		return this;
	}
	
	
	/**
	 * 显示的列名
	 * @author ls
	 * 2014年11月25日上午10:31:57 
	 * @return
	 */
	public String getShowName(){
		if(showName==null || "".equals(showName)){
			showName = ( table!=null?table.getAlias()+"."+this.getAlias():this.getAlias());
		}
		return showName;
	}
	
	private String getShowName(String alias){
		return table!=null?table.getAlias()+"."+alias:alias;
	}
	
	/**
	 * 是否为显示全部列
	 * @author ls
	 * 2014年11月25日上午10:34:43 
	 * @return
	 */
	public boolean isShowAll(){
		if(this.getShowName().equals(this.getShowName("*"))){
			return true;
		}
		return false;
	}
	
	public void accept(FieldVisitor visitor) {
		visitor.visit(this);
	}

}
